﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace sdf.Code
{
	public abstract class Ui_Base : System.Web.UI.Page
	{
		protected abstract GridView page_GridView { get; }
		protected abstract DetailsView page_DetailsView { get; }
		protected abstract DropDownList page_DataSourceDropDown { get; }
		protected abstract DropDownList page_Param1DropDown { get; }
		protected abstract Label page_Param1Label { get; }
		protected abstract Button page_GoButton { get; }

		protected override void OnInit(EventArgs e)
		{
			//update master page title


			// Be sure to call the base class's OnLoad method!
			base.OnInit(e);
		}


		protected void ddDataSource_SelectedIndexChanged(object sender, EventArgs e)
		{
			UpdateGridDataSource();
		}

		protected void ddParam1_SelectedIndexChanged(object sender, EventArgs e)
		{

		}



		protected void btnAdd_Click(object sender, EventArgs e)
		{
			BaseChangeToInsertMode(sender, e);
		}

		protected override void OnLoad(EventArgs e)
		{
			//update master page title
			Label lblMasterTitle = (Label) Master.FindControl("lblMasterTitle");
			lblMasterTitle.Text = Page.Title;

			string sFk = Request.QueryString["fk"];
			if (!string.IsNullOrEmpty(sFk))
			{
				page_DataSourceDropDown.Items.FindByText("By " + sFk).Selected = true;
				page_Param1DropDown.SelectedValue = Request.QueryString["value"];
				UpdateGridDataSource();
			}


			// Be sure to call the base class's OnLoad method!
			base.OnLoad(e);
		}

		/// <summary>
		///		Finds the "modified_user" param in the command Parameters collection and sets
		///		it to the current Identity name.
		/// </summary>
		/*
		public static void UpdateUserParam(Object sender, System.Web.UI.WebControls.SqlDataSourceCommandEventArgs e)
		{
			foreach (System.Data.Common.DbParameter param in e.Command.Parameters)
			{
				if (param.ParameterName == "@modified_user")
				{
					param.Value = HttpContext.Current.User.Identity.Name;
				}
			}
		}
		*/
 
		/// <summary>
		///		Finds the "modified_user" param in the command Parameters collection and sets
		///		it to the current Identity name.
		/// </summary>
		public void UpdateParams(Object sender, System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs e)
		{
			if (e.InputParameters["ModifiedUser"] != null)
			{
				e.InputParameters["ModifiedUser"] = HttpContext.Current.User.Identity.Name;
			}
	
			//ConvertNullParamToEmptyString(sender, e, "@admin_comment");
		}

		/// <summary>
		///		Finds the "modified_user" param in the command Parameters collection and sets
		///		it to the current Identity name.
		/// </summary>
		/// <history>
		///		[RConley] 11/02/2005 Created
		/// </history>
		public static void ConvertNullParamToEmptyString(Object sender, System.Web.UI.WebControls.SqlDataSourceCommandEventArgs e, string sParamName)
		{
			foreach (System.Data.Common.DbParameter param in e.Command.Parameters)
			{
				if (param.ParameterName == sParamName)
				{
					if (param.Value == null)
					{
						param.Value = string.Empty;
					}
				}
			}
		}



		protected void dvItemInserted(object sender, DetailsViewInsertedEventArgs e)
		{
			page_GridView.DataBind();
		}

		protected void dvItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
		{
			page_GridView.DataBind();
		}
		protected void dvItemDeleted(object sender, DetailsViewDeletedEventArgs e)
		{
			page_GridView.DataBind();
		}

		protected void BaseChangeToInsertMode(object sender, EventArgs e)
		{
			page_DetailsView.Visible = true;
			page_DetailsView.ChangeMode(DetailsViewMode.Insert);
		}

		/// -------------------------------------------------------------------
		/// <summary>
		///		Event handling
		/// </summary>
		/// -------------------------------------------------------------------


		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void gvOnDataBound(object sender, EventArgs e)
		{
		}

		protected void UpdateControlsOnGridviewIndexChange()
		{
			

		}


		protected void UpdateGridDataSource()
		{
			string sNewGridDataSourceId = "dataGridSource" + (page_DataSourceDropDown.SelectedIndex + 1).ToString();

			if (page_GridView.DataSourceID != sNewGridDataSourceId)
			{
				page_GridView.AllowSorting = (page_DataSourceDropDown.SelectedIndex == 0);
				page_GridView.DataSourceID = sNewGridDataSourceId;
				page_GridView.DataBind();

				if (page_DataSourceDropDown.SelectedIndex < 2)
				{
					page_Param1DropDown.DataSourceID = null;
				}
			}

			//hide all param controls
			page_Param1Label.Visible = page_Param1DropDown.Visible = page_GoButton.Visible = false;

			
			if (page_DataSourceDropDown.SelectedIndex > 1)
			{
				page_GoButton.Visible = true;
				string[] arTopSplit = page_DataSourceDropDown.SelectedValue.Split('^');
				string[] arKeyNames = arTopSplit[0].Split('~');
				string[] arTextFieldNames = arTopSplit[1].Split('~');

				string sParamName = arKeyNames[0];
				string sNewDropDownDataSourceId = "dataDropDown1Source" + (page_DataSourceDropDown.SelectedIndex + 1).ToString();

				page_Param1Label.Visible = true;
				page_Param1Label.Text = sParamName;
				page_Param1DropDown.Visible = true;

				page_Param1DropDown.DataSourceID = sNewDropDownDataSourceId;
				page_Param1DropDown.DataTextField = arTextFieldNames[0];
				page_Param1DropDown.DataValueField = sParamName;
				page_Param1DropDown.DataBind();
			}

		}
	}
}
